package com.example.chuntiao.cophook.ui;

import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.example.chuntiao.cophook.R;

/**
 * Created by chuntiao on 17-8-30.
 */

public class InputDialog {
    private AlertDialog alertDialog;
    private OnPositiveListener listener;
    EditText mEditText;
    TextView mLinkText;

    /**
     * 这是生成输入对话框的封装类
     * @param context
     * @param title 输入框标题
     * @param linkedText 与之关联的输入控件
     * @param change 按下确定时被置为true*/
    public InputDialog(Context context,String title, final TextView linkedText,final Boolean[] change) {
        View rootView = LayoutInflater.from(context).inflate(R.layout.input_dialog, null);
        mEditText = (EditText) rootView.findViewById(R.id.edit_info);
        mLinkText=linkedText;
        alertDialog = new AlertDialog.Builder(context)
                .setTitle(title)
                .setView(rootView)
                .setNegativeButton(R.string.cancel, null)
                .setPositiveButton(R.string.yse, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        linkedText.setText(mEditText.getText());
                        change[0]=true;
                        if(listener!=null){
                            listener.onPositiveClick();
                        }
                    }
                })
                .create();
    }
    /**
     * 这是生成对话框的封装类
     * @param context
     * @param title 输入框标题
     * @param linkedText 与之关联的输入控件
     * @param change 按下确定时被置为true
     * @param type 输入框类型，用来约束键盘*/
    public InputDialog(Context context, String title, final TextView linkedText, final Boolean[] change, int type) {
        View rootView = LayoutInflater.from(context).inflate(R.layout.input_dialog, null);
        mEditText = (EditText) rootView.findViewById(R.id.edit_info);
        mLinkText=linkedText;
        mEditText.setInputType(type);
        alertDialog = new AlertDialog.Builder(context)
                .setTitle(title)
                .setView(rootView)
                .setNegativeButton(R.string.cancel, null)
                .setPositiveButton(R.string.yse, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if(linkedText!=null)
                            linkedText.setText(mEditText.getText());

                        change[0]=true;
                        if(listener!=null){
                            listener.onPositiveClick();
                        }
                    }
                })
                .create();
    }
    public InputDialog(Context context, String title, final TextView linkedText) {
        View rootView = LayoutInflater.from(context).inflate(R.layout.input_dialog, null);
        mEditText = (EditText) rootView.findViewById(R.id.edit_info);
        mLinkText=linkedText;
        alertDialog = new AlertDialog.Builder(context)
                .setTitle(title)
                .setView(rootView)
                .setNegativeButton(R.string.cancel, null)
                .setPositiveButton(R.string.yse, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if(linkedText!=null)
                            linkedText.setText(mEditText.getText());

                        if(listener!=null){
                            listener.onPositiveClick();
                        }
                    }
                })
                .create();
    }
    public InputDialog(Context context, String title, String msg ) {
        View rootView = LayoutInflater.from(context).inflate(R.layout.input_dialog, null);
        mEditText = (EditText) rootView.findViewById(R.id.edit_info);
        alertDialog = new AlertDialog.Builder(context)
                .setTitle(title)
                .setMessage(msg)
                .setView(rootView)
                .setNegativeButton(R.string.cancel, null)
                .setPositiveButton(R.string.yse, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        if(listener!=null){
                            listener.onPositiveClick();
                        }
                    }
                })
                .create();
    }

    public void show(){
        alertDialog.show();
    }
    public void setText(CharSequence txt){
        mEditText.setText(txt);
    }
    public String getString(){
        return mEditText.getText().toString();
    }
    public interface OnPositiveListener{
        void onPositiveClick();
    }

    public void setListener(OnPositiveListener listener) {
        this.listener = listener;
    }
    public void setSelection(int index){
        mEditText.setSelection(index);
    }
}
